﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnheimGallery.DAL
{
    public class CategoryInfo
    {
        public string Name;
    }
    public class CategoryContentData
    {
        public List<Category> subcategories;
        public List<Photo> photos;
    }
    public class CategoriesDAL
    { 
        public static void AddRootCategory(aspnet_Users user)
        {
            EnheimGalleryDatabaseEntities db = new EnheimGalleryDatabaseEntities();
           
            Category newCategory = new Category()
            {
                Name = user.UserName,
                OwnerId = user.UserId,
                AddedDate=DateTime.Now,
                ParentCategoryId=null
            };
            db.AddToCategories(newCategory);
            db.SaveChanges();
        }
        public static void AddSubCategoryToUser(aspnet_Users user, Category parent, string name)
        {
            EnheimGalleryDatabaseEntities db = new EnheimGalleryDatabaseEntities();
            Category newCategory = new Category()
            {
                Name = name,
                OwnerId = user.UserId,
                ParentCategoryId = parent.Id,
                AddedDate = DateTime.Now
            };
            db.AddToCategories(newCategory);
            db.SaveChanges();
        }
        public CategoryContentData GetCategoryData(Category category)
        {
            EnheimGalleryDatabaseEntities db = new EnheimGalleryDatabaseEntities();
            var subcategories = from subcategory in db.Categories
                      where subcategory.ParentCategoryId == category.Id
                      select subcategory;

            var photos = from photo in db.Photos
                         where photo.CategoryId == category.Id
                         select photo;
            
            CategoryContentData content = new CategoryContentData();
            content.subcategories = subcategories.ToList();
            content.photos = photos.ToList();

            return content;
        }
        public static void RenameCategory(Category category, string newName)
        {
            EnheimGalleryDatabaseEntities db = new EnheimGalleryDatabaseEntities();
            Category modifiedCategory=category;
            modifiedCategory.Name = newName;
            db.DeleteObject(category);
            db.AddToCategories(modifiedCategory);
            db.SaveChanges();
        }
        public static void DeleteCategory(Category category)
        {
            EnheimGalleryDatabaseEntities db = new EnheimGalleryDatabaseEntities();
             // subcategories and photos, recursive
        }
    }
}
